﻿using Acme.BookStore.RBAC;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using SC.UserCenter.Infra;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Acme.BookStore.Users
{
    [ApiExplorerSettings(GroupName = "v2")]
    public class UserServices : ApplicationService, IUserServices
    {
        IRepository<User> _userInfoRepository;
        IRepository<UserClients> _userClientsRepository;
        IRepository<ClientInfo> _clientInfoRepository;

        public UserServices(IRepository<User> userInfoRepository, IRepository<UserClients> userClientsRepository, IRepository<ClientInfo> clientInfoRepository)
        {
            _userInfoRepository = userInfoRepository;
            _userClientsRepository = userClientsRepository;
            _clientInfoRepository = clientInfoRepository;
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="infoDTO">实体</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<ApiResult> UserAdd(UserInfoDTO infoDTO)
        {
            try
            {
                var lisr = ObjectMapper.Map<UserInfoDTO, User>(infoDTO);

                await _userInfoRepository.InsertAsync(lisr);
                return ApiResult.Ok(data: lisr);
            }
            catch (Exception ex)
            {
                ex.HelpLink = "操作失败";
                return ApiResult.Fail(msg: ex.Message);
            }
        }
        /// <summary>
        /// 注册用户
        /// </summary>
        /// <param name="infoDTO"></param>
        /// <returns></returns>
        [HttpPost("UserClienAdd")]
        public async Task<ApiResult> UserClienAdd(UserClientsDTO infoDTO)
        {
            var entity = ObjectMapper.Map<UserClientsDTO, UserClients>(infoDTO);
            await _userClientsRepository.InsertAsync(entity);
            return ApiResult.Ok();
        }
        /// <summary>
        /// 完善个人信息资料
        /// </summary>
        /// <param name="infoDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> UserClienInfor(UserClienInforDTO infoDTO)
        {
            var entity = ObjectMapper.Map<UserClienInforDTO, ClientInfo>(infoDTO);
            var list= await _clientInfoRepository.FirstOrDefaultAsync(x=>x.UseId==entity.UseId);
            if (list != null)
            {
                return ApiResult.Fail("已经完善了");
            }
            else
            {
                await _clientInfoRepository.InsertAsync(entity);
                return ApiResult.Ok();

            }
        }


    }
}
